/**
 * Created by Administrator on 2017/1/17.
 */
var SCENIC;
var SHOP;
var reservearray = [];
var shopArray = [];
var LOGIN;
var contact;
var issingle;
// var CHOOSE, SPOT;
var CART, COMFIRMORDER;
var MINEINFO, MINEPRO, MINEORDER, MINEOPINION, ADMINPEND, MINEEXPEND, PENDORDER;
define(function (require, exports, module) {
    'use strict';
    Date.prototype.Format = function (fmt) { //author: meizz
        var o = {
            "M+": this.getMonth() + 1,                 //月份
            "d+": this.getDate(),                    //日
            "h+": this.getHours(),                   //小时
            "m+": this.getMinutes(),                 //分
            "s+": this.getSeconds(),                 //秒
            "q+": Math.floor((this.getMonth() + 3) / 3), //季度
            "S": this.getMilliseconds()             //毫秒
        };
        if (/(y+)/.test(fmt))
            fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
        for (var k in o)
            if (new RegExp("(" + k + ")").test(fmt))
                fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
        return fmt;
    }

    var initApp = function () {
        // //初始化签证对象
        // //初始化签证对象
        $.ajax({
            type: "GET",
            url: "/user/usermessage?userid="+user.id,
            dataType: "json",
            success: function (res) {
                if((res.data[0].telephone!='' && res.data[0].telephone!='')){
                    $("#homepage").attr('src', "images/bar/homepagel.png");
                    $("#shopcart").attr('src', "images/bar/shopcart.png");
                    $("#mine").attr('src', "images/bar/barcenter.png");
                    $("#shouye").addClass("on-mine");
                    $("#gouwuche").removeClass("on-mine");
                    $("#wode").removeClass("on-mine");
                    spotView();
                }else {
                    seajs.use('/script/login', function (login) {
                        $('#scenicbody').html(new EJS({url: "views/login.ejs"}).render({

                        }));
                        LOGIN=login;
                    })
                }
            }
        })
        $.get('/data/getTicket?spotid=AT009&url=' + encodeURIComponent(window.location.href.split('#')[0]), function (result) {
            //console.log(result)
            if (!result.success) toast.show(result.msg);
            var sign = result.sign;
            wx.config({
                debug: false,
                appId: sign.appid,
                timestamp: sign.timestamp,
                nonceStr: sign.noncestr,
                signature: sign.signature,
                jsApiList: [
                    'previewImage',
                    'chooseImage',
                    'uploadImage',
                    'chooseWXPay',
                    'onMenuShareTimeline',
                    'onMenuShareAppMessage',
                    'onMenuShareQQ',
                    'onMenuShareWeibo',
                    'onMenuShareQZone',
                    'getLocation',
                    'startRecord',
                    'stopRecord',
                    'translateVoice'
                ]
            });

            wx.ready(function () {
                wx.onMenuShareAppMessage({
                    title: title + "e游",
                    desc: '我在' + title + '，这里有最火的线上线下游玩体验，赶快来吧！',
                    link: window.location.href,
                    imgUrl: user.headurl
                });
                //分享到朋友圈
                wx.onMenuShareTimeline({
                    title: '我在' + title + '，这里有最火的线上线下游玩体验，赶快来吧！',
                    desc: '我在' + title + '，这里有最火的线上线下游玩体验，赶快来吧！',
                    link: window.location.href,
                    imgUrl: user.headurl
                });
                //分享到QQ
                wx.onMenuShareQQ({
                    title: title + "e游",
                    desc: '我在' + title + '，这里有最火的线上线下游玩体验，赶快来吧！',
                    link: window.location.href,
                    imgUrl: user.headurl
                });
            })

        });
    }

    function spotView() {
        $.get('/scenicspot/sceniclist', function (res) {
            seajs.use("/script/scenicspot/sceniclist", function (scenic) {
                $("#scenicbody").html(new EJS({url: "/views/scenicspot/sceniclist.ejs"}).render({
                    datas: res.datas.rows1,
                    datas2: res.datas.rows2
                }));
                SCENIC = scenic;
                SCENIC.chooseinit();
            });
        })
    }



    // function addsceniclist() {
    //     var scenicname = $('#scenicname').val();
    //     var scenicdes = $('#scenicdes').val();
    //     $.ajax({
    //         url: '/scenicspot/addsceniclist',
    //         type: 'POST',
    //         data: {
    //             scenicname:scenicname,
    //             scenicdes:scenicdes,
    //             userid:user.id
    //         },
    //         success: function (res) {
    //             if (!res.success) {
    //                 alert(res.msg);
    //                 return;
    //             }else{
    //                 alert('成功!')
    //             }
    //         }
    //     })
    // }


    function timechoose() {
        $('.backcolor').show();
        $('#sizechoose').show();
    }

    function shoplist(sid,time,timesize) {
        var standard = $('#chooseresult').html();
        var timesinchoose = $('#timesinchoose').val();
        var timemulchoose = $('#timemulchoose').val();
        var starttime = '';
        var endtime = '';
        if (timesinchoose) {
            starttime = new Date(timesinchoose);
        } else {
            var timechoose = timemulchoose.split('-');
            starttime = new Date(timechoose[0]);
            endtime = new Date(timechoose[1]);
        }
        if(time.indexOf('-')>-1){
            issingle = false;
        }else{
           issingle = true;
        }
        $.get('/scenicspot/shoplist?sid=' + 'AT016', function (res) {
            var datas = res.datas;
            // for (var i = 0; i < datas.length; i++) {
            //     datas[i].ischoose = false;
            //     datas[i].istime = false;
            //     var pdate = new Date(datas[i].starttime);
            //     var edate = new Date(datas[i].endtime);
            //     pdate = new Date(pdate.getFullYear(), pdate.getMonth(), pdate.getDate());
            //     edate = new Date(edate.getFullYear(), edate.getMonth(), edate.getDate());
            //     starttime = new Date(starttime.getFullYear(), starttime.getMonth(), starttime.getDate());
            //     if (endtime) {
            //         endtime = new Date(endtime.getFullYear(), endtime.getMonth(), endtime.getDate());
            //     }
            //     var reg = /\d+/g;
            //     var tempP = pdate.toString().match(reg);
            //     var tempE = edate.toString().match(reg);
            //     var tempST = starttime.toString().match(reg);
            //     var tempET = endtime.toString().match(reg);
            //     tempP = new Date(tempP[0], parseInt(tempP[1]) - 1, tempP[2]);
            //     tempE = new Date(tempE[0], parseInt(tempE[1]) - 1, tempE[2]);
            //     tempST = new Date(tempST[0], parseInt(tempST[1]) - 1, tempST[2]);
            //     if (tempET) {
            //         tempET = new Date(tempET[0], parseInt(tempET[1]) - 1, tempET[2]);
            //     }
            //     if (tempE >= tempST) {
            //         datas[i].istime = true;
            //         if (datas[i].standard == standard) {
            //             datas[i].ischoose = true;
            //         }
            //     }
            // }
            seajs.use("/script/scenicspot/shoplist", function (shop) {
                $("#scenicbody").html(new EJS({url: "/views/scenicspot/shoplist.ejs"}).render({
                    datas: datas,
                    time:time,
                    timesize:timesize
                }));
                SHOP = shop;
                // SHOP.adddel();
                SHOP.shoplistinit();
                SHOP.calendar();
            });
        })
    }

    //所有景区列表
    function allshop(spotid) {
        $.get('/scenicspot/findgoods?timeone=' +new Date()+'&startDate='+''+'&endDate='+''+'&timezone='+''+'&type='+''+'&spotid='+spotid, function (res) {
            // shopArray = res.data;
            seajs.use("/script/scenicspot/shoplist", function (shop) {
                $("#scenicbody").html(new EJS({url: "/views/scenicspot/shoplist.ejs"}).render({
                    datas: res.datas,
                    time:'',
                    timesize:'活动时长'
                }));
                SHOP = shop;
            });
        })
    }



    //购物车
    function shoppingCart() {
        $("#homepage").attr('src', "images/bar/homepage.png");
        $("#shopcart").attr('src', "images/bar/shopcartl.png");
        $("#mine").attr('src', "images/bar/barcenter.png");
        $("#shouye").removeClass("on-mine");
        $("#gouwuche").addClass("on-mine");
        $("#wode").removeClass("on-mine");
        $.get('/cart/shoppingcart?userid=' + user.id, function (res) {
            shopArray = res.data;
            seajs.use("/script/shoppingcart/shoppingcart", function (cart) {
                $("#scenicbody").html(new EJS({url: "/views/shoppingcart/shoppingcart.ejs"}).render({
                    data: res.data
                }));
                CART = cart;
            });
        })
    }

    //确认订单
    function comfirmOrder() {
        var count = '';
        seajs.use("/script/shoppingcart/comfirmorder", function (comfirm) {
            $("#scenicbody").html(new EJS({url: "/views/shoppingcart/comfirmorder.ejs"}).render({
                data: reservearray,
                count: count
            }));
            COMFIRMORDER = comfirm;
        });
    }



    //我的
    function mineInfo() {
        $("#homepage").attr('src', "images/bar/homepage.png");
        $("#shopcart").attr('src', "images/bar/shopcart.png");
        $("#mine").attr('src', "images/bar/barcenterl.png");
        $("#shouye").removeClass("on-mine");
        $("#gouwuche").removeClass("on-mine");
        $("#wode").addClass("on-mine");

        $.get('/mine/mineproinfo?userid=' + user.id, function (res) {
            if (res.data.length > 0) {
                seajs.use("/script/mine/mineinfo", function (mineinfo) {
                    $("#scenicbody").html(new EJS({url: "/views/mine/mineinfo.ejs"}).render({
                        data: res.data
                        // activityWidth: activityWidth,
                        // activityHeight: activityHeight
                    }));
                    MINEINFO = mineinfo;
                });
            }
        })
    }



//我的订单
    function mineOrder() {

        $.get('/mine/mineorder?userid=' + user.id, function (res) {
            seajs.use("/script/mine/mineorder", function (mineorder) {
                $("#scenicbody").html(new EJS({url: "/views/mine/mineorder.ejs"}).render({
                    data: res.data.rows1,
                    data2: res.data.rows2
                }));
                MINEORDER = mineorder;
            });
        })
    }

    //我的消费
    function mineExpend() {
        $.get('/mine/mineexpend?userid=' + user.id, function (res) {
            seajs.use("/script/mine/mineexpend", function (mineexpend) {
                $("#scenicbody").html(new EJS({url: "/views/mine/mineexpend.ejs"}).render({
                    allmoney: res.data.allmoney,
                    data: res.data.rows,
                    data_s: res.data.data_s
                }));

                MINEEXPEND = mineexpend;
            });
            // }
        })
    }

    //管理员待处理订单
    function adminPending() {
        $.get('/mine/adminpending', function (res) {
            seajs.use("/script/mine/adminpending", function (adminPending) {
                $("#scenicbody").html(new EJS({url: "/views/mine/adminpending.ejs"}).render({
                    data: res.data.rows1,
                    data2: res.data.rows2
                }));
                ADMINPEND = adminPending;
            });
        })
    }

    //待处理订单详情
    function pendingOrder(userid, id) {
        $.get('/mine/pendingorder?id=' + id + "&userid=" + userid, function (res) {
            if (res.data.length > 0) {
                seajs.use("/script/mine/pendingorder", function (pendingOrder) {
                    $("#scenicbody").html(new EJS({url: "/views/mine/pendingorder.ejs"}).render({
                        data: res.data
                    }));
                    PENDORDER = pendingOrder;
                });
            }
        })
    }

    //我的意见
    function mineOpinion() {
        seajs.use("/script/mine/myopinion", function (mineopinion) {
            $("#scenicbody").html(new EJS({url: "/views/mine/myopinion.ejs"}).render({}));
            MINEOPINION = mineopinion;
        });
    }

    return {
        initApp: initApp,
        spotView: spotView,
        shoplist: shoplist,
        shoppingCart: shoppingCart,
        comfirmOrder: comfirmOrder,
        mineInfo: mineInfo,
        mineOrder: mineOrder,
        mineExpend: mineExpend,
        pendingOrder: pendingOrder,
        mineOpinion: mineOpinion,
        adminPending: adminPending,
        timechoose: timechoose,
        allshop:allshop
        // addsceniclist:addsceniclist,

    }
});


